package com.api.devices.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.api.common.config.ApiConfig;
import com.api.common.enums.DataSourceType;
import com.api.common.utils.MessageUtils;
import com.api.devices.domain.query.MonitoredLatestQuery;
import com.api.devices.domain.query.MonitoredQuery;
import com.api.devices.domain.query.intergration.ElementLatestScoreQuery;
import com.api.devices.domain.query.intergration.RobotLatestScoreQuery;
import com.api.devices.domain.query.tracing.intergration.IntergrationQuery;
import com.api.devices.domain.vo.MonitoredVo;
import com.api.devices.domain.vo.tracing.intergration.IntergrationVo;
import com.api.devices.domain.vo.tree.TreeVo;
import com.api.devices.mapper.IntergrationMapper;
import com.api.devices.mapper.MonitoredMapper;
import com.api.devices.service.IIntergrationService;
import com.api.framework.datasource.DynamicDataSourceContextHolder;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class IntergrationServiceImpl implements IIntergrationService {
    @Autowired
    IntergrationMapper intergrationMapper;
    @Autowired
    MonitoredMapper monitoredMapper;

    @Override
    public List<Double> generates(TreeVo vo) {
        List<Double> dataList = new ArrayList<>();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            dataList = intergrationMapper.generates(vo.getIntergrationTabName(), vo);
            DynamicDataSourceContextHolder.clearDataSourceType();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }

    @Override
    public List<IntergrationQuery> scroe(IntergrationVo vo) {
        List<IntergrationQuery> dataList = new ArrayList<>();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            vo.initTableName();
            dataList = intergrationMapper.scroe(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return dataList;
    }

    @Override
    public IntergrationQuery series(IntergrationVo vo) {
        IntergrationQuery data = new IntergrationQuery();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            vo.initTableName();
            data = intergrationMapper.series(vo);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return data;
    }

    @Override
    public Map<String, Object> scoreLast(List<String> monitoredCodeList) {
        List<MonitoredLatestQuery> list = monitoredMapper.itemsCode(monitoredCodeList);
        Map<String, Object> map = new HashMap<>();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            for (MonitoredLatestQuery item : list) {
                try {
//                String tableName = ApiConfig.getTenant() + "_" + item.getMonitoredCode() + "_tracing_intergration";
                    String tableName = item.getMonitoredCode() + "_tracing_intergration";
                    List<Map> rl = intergrationMapper.intergrationLastData(tableName);
                    item.setData(rl);
                    map.put(item.getMonitoredCode(), item);
                } catch (Exception e) {
                    JSONObject paramJO = new JSONObject();
                    paramJO.put("error", true);
                    paramJO.put("msg", MessageUtils.message("common.run.error") + ":" + e.getMessage());
                    map.put(item.getMonitoredCode(), paramJO);
                }
            }
        } catch (RuntimeException e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return map;
    }

    @Override
    public List<RobotLatestScoreQuery> scoreRobotList(List<String> robotCodes) {
        List<RobotLatestScoreQuery> list = intergrationMapper.getRobotList(robotCodes);
        for (RobotLatestScoreQuery rScore : list) {
            String robotCode = rScore.getRobotCode();
            List<ElementLatestScoreQuery> rChildren = rScore.getChildren();
            if (ObjectUtils.isNotEmpty(rChildren)) {
                for (ElementLatestScoreQuery subChild : rChildren) {
                    String tableName = subChild.getIntergrationName(
                            "002", "001"
                    );
                    try {
                        subChild.setData(intergrationLastData(tableName));
                    } catch (Exception e) {
                    }
                    ;
                }
            }
        }
        return list;
    }

    @Override
    public List<RobotLatestScoreQuery> scoreRobotIds(List<Long> robotIds) {
        List<RobotLatestScoreQuery> list = intergrationMapper.scoreRobotIds(robotIds);
        for (RobotLatestScoreQuery rScore : list) {
            String robotCode = rScore.getRobotCode();
            List<ElementLatestScoreQuery> rChildren = rScore.getChildren();
            if (ObjectUtils.isNotEmpty(rChildren)) {
                for (ElementLatestScoreQuery subChild : rChildren) {
                    String tableName = subChild.getIntergrationName(
                            "002", "001"
                    );
//                    try {
                        subChild.setData(intergrationLastData(tableName));
//                    } catch (Exception e) {
//                    }
                }
            }
        }
        return list;
    }

    private List<Map> intergrationLastData(String tableName) {
        List<Map> list = new ArrayList<>();
        try {
            DynamicDataSourceContextHolder.setDataSourceType(DataSourceType.SLAVE.name());
            list = intergrationMapper.intergrationLastData(tableName);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            DynamicDataSourceContextHolder.clearDataSourceType();
        }
        return list;
    }
}
